<?php
/**
 * AnniversaryService.php
 * 文件描述 纪念日逻辑处理中心
 * Created on 2020/8/31 11:48
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\xiangji\services;

use think\facade\Filesystem;
use think\File;

class FileService
{
    private $store;

    public function __construct()
    {
        $this->store = Filesystem::disk(config('filesystem.default'));
    }

    public function deleteDir(string $dir)
    {
        return $this->store->deleteDir($dir);
    }

    public function moveFile($from, $to)
    {
        if (!$this->store->has($from)) return false;
        $this->store->copy($from, $to);
        return $this->store->delete($from);
    }

    public function uploadImage(File $file)
    {
        return $this->uploadFile($file, 'images');
    }

    public function uploadTemp(File $file)
    {
        return $this->uploadFile($file);
    }

    private function uploadFile(File $file, string $path = 'temp')
    {
        $url = $this->store->putFile($path, $file);
        return ['url' => $this->store->getUrl($url)];
    }

    public function uploadShareCode($file)
    {
        return $this->uploadFile($file, 'wechat/share/code');
    }
}
